ReactNative 絶対パスでモジュールをimportする
#ReactNative
概要
相対パスは嫌だって人向け。
code: sample.js
import { Sample } from '../../../../sample.js';
モジュールのimportを絶対パスで行いたい時の方法。
やり方はいくつかあるらしいが、今回は一番シンプルなものだけ記載。
package.jsonを使う方法
以下のようにnameを指定する
code: package.json
{
"name": "hogehoge"
}
その上で絶対パスでimportする
code: sample.react.js
import { Sample } from 'hogehoge/src/components/sample.js';
以下のようなディレクトリ構成を想定
code: dir
.
- src/
- components/
- sample.js
参考
絶対パスでモジュールをimportする - Qiita
React Native で画像を require() する時の注意点 - Qiita